package nsu.edu.zsq.service.impl.teacher;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import nsu.edu.zsq.bean.StuScore;
import nsu.edu.zsq.common.RequestHolder;
import nsu.edu.zsq.dao.StuScoreMapper;
import nsu.edu.zsq.dao.TchCourseMapper;
import nsu.edu.zsq.param.PageQuery;
import nsu.edu.zsq.param.StuScoreParam;
import nsu.edu.zsq.param.TchCourseParam;
import nsu.edu.zsq.service.sys.SysLogService;
import nsu.edu.zsq.service.teacher.CourseScoreService;
import nsu.edu.zsq.util.BeanValidator;

/** 
* Description: 教师课程成绩管理的service实现类<br>
*/
@Service
public class CourseScoreServiceImpl implements CourseScoreService{

    @Autowired
    private TchCourseMapper tchCourseMapper;
    @Autowired
    private StuScoreMapper stuScoreMapper;
    @Autowired
    private SysLogService sysLogService;
    
    @Override
    public StuScore getStuScore(int id) {
        StuScore stuScore = stuScoreMapper.selectByPrimaryKey(id);
        return stuScore;
    }
    
    @Override
    public List<TchCourseParam> listMajorTchCourse() {
        List<TchCourseParam> listMajorTchCourse = tchCourseMapper.listMajorTchCourse(RequestHolder.getCurrentTeacher().getTeacherNo());
        return listMajorTchCourse;
    }

    @Override
    public PageInfo<StuScoreParam> listStuScoreByClassId(int classId, String courseNo, PageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPageNum(),pageQuery.getPageSize());
        List<StuScoreParam> listStuScore = stuScoreMapper.listStuScore(classId, courseNo);
        PageInfo<StuScoreParam> pageInfo = new PageInfo<StuScoreParam>(listStuScore, pageQuery.getNavigatePages());
        return pageInfo;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveStuScore(StuScore stuScore) {
        BeanValidator.check(stuScore);
        if (stuScore.getId() != null) {
            // 1.如果id不为空，代表为更新操作
            StuScore before = getStuScore(stuScore.getId());
            StuScore after = StuScore.builder().id(before.getId()).score(stuScore.getScore())
                    .operator(RequestHolder.getCurrentTeacher().getTeacherNo())
                    .operatorTime(new Date()).build();
            // 如果成绩不及格，自动加入补考标记
            if (stuScore.getScore() < 60) {
                after.setResitStatus(1);
            } else {
                after.setResitStatus(0);
            }
            stuScoreMapper.updateByPrimaryKeySelective(after);
            
            // 添加日志
            String instruction = "主修课成绩管理-编辑成绩";
            sysLogService.saveSysLogByTeacher(before, after, instruction);
            return;
        }
        // 2.否则添加操作
        
        // 如果成绩不及格，自动加入补考标记
        if (stuScore.getScore() < 60) {
            stuScore.setResitStatus(1);
        }
        stuScore.setOperator(RequestHolder.getCurrentTeacher().getTeacherNo());
        stuScore.setOperatorTime(new Date());
        stuScoreMapper.insertSelective(stuScore);
        
        // 添加日志
        String instruction = "主修课成绩管理-编辑成绩";
        sysLogService.saveSysLogByTeacher(null, stuScore, instruction);
    }

    @Override
    public List<TchCourseParam> listMinorTchCourse() {
        List<TchCourseParam> listMinorTchCourse = tchCourseMapper.listMinorTchCourse(RequestHolder.getCurrentTeacher().getTeacherNo());
        return listMinorTchCourse;
    }

    @Override
    public PageInfo<StuScoreParam> listStuScoreByTchCourseId(int tchCourseId, PageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPageNum(),pageQuery.getPageSize());
        List<StuScoreParam> listStuScore = stuScoreMapper.listStuScoreByTchCourseId(tchCourseId);
        PageInfo<StuScoreParam> pageInfo = new PageInfo<StuScoreParam>(listStuScore, pageQuery.getNavigatePages());
        return pageInfo;
    }

    @Override
    public PageInfo<StuScoreParam> listResitStuScore(PageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPageNum(),pageQuery.getPageSize());
        List<StuScoreParam> listResitStuScore = stuScoreMapper.listResitStuScore(RequestHolder.getCurrentTeacher().getTeacherNo());
        PageInfo<StuScoreParam> pageInfo = new PageInfo<StuScoreParam>(listResitStuScore, pageQuery.getNavigatePages());
        return pageInfo;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateResitScore(StuScore stuScore) {
        StuScore before = getStuScore(stuScore.getId());
        StuScore after = StuScore.builder().id(before.getId()).resitScore(stuScore.getResitScore())
                .operator(RequestHolder.getCurrentTeacher().getTeacherNo())
                .operatorTime(new Date()).build();
        // 如果补考成绩不及格，自动加入重修标记
        if (stuScore.getResitScore() < 60) {
            after.setRetakeStatus(1);
        } else {
            after.setRetakeStatus(0);
        }
        stuScoreMapper.updateByPrimaryKeySelective(after);
        
        // 添加日志
        String instruction = "补考成绩管理-编辑成绩";
        sysLogService.saveSysLogByTeacher(null, stuScore, instruction);
    }

    @Override
    public PageInfo<StuScoreParam> listRetakeStuScore(PageQuery pageQuery) {
        PageHelper.startPage(pageQuery.getPageNum(),pageQuery.getPageSize());
        List<StuScoreParam> listResitStuScore = stuScoreMapper.listRetakeStuScore(RequestHolder.getCurrentTeacher().getTeacherNo());
        PageInfo<StuScoreParam> pageInfo = new PageInfo<StuScoreParam>(listResitStuScore, pageQuery.getNavigatePages());
        return pageInfo;
    }

    @Override
    public void updateRetakeScore(StuScore stuScore) {
        StuScore before = getStuScore(stuScore.getId());
        StuScore after = StuScore.builder().id(before.getId()).retakeScore(stuScore.getRetakeScore())
                .operator(RequestHolder.getCurrentTeacher().getTeacherNo())
                .operatorTime(new Date()).build();
        stuScoreMapper.updateByPrimaryKeySelective(after);
        
        // 添加日志
        String instruction = "重修成绩管理-编辑成绩";
        sysLogService.saveSysLogByTeacher(null, stuScore, instruction);
        
    }

    @Override
    public List<StuScoreParam> listRetakeCourse() {
        List<StuScoreParam> listRetakeCourse = stuScoreMapper.listRetakeCourse(RequestHolder.getCurrentTeacher().getTeacherNo());
        return listRetakeCourse;
    }

    @Override
    public List<StuScoreParam> listRetakeStudentByTchCourseId(int tchCourseId) {
        List<StuScoreParam> listRetakeStudent = stuScoreMapper.listRetakeStudent(tchCourseId);
        return listRetakeStudent;
    }

}
